package com.wyl.niupi.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import jdk.nashorn.internal.runtime.options.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author 吴宇亮 on 2020/5/15 0015 下午 12:00
 */
@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.durid-datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }

    //springboot获取yml的几种方式：https://www.cnblogs.com/mysgk/p/9790801.html
    @Autowired
    private Environment env;

    /**
     * 配置Durid监控后台的Servlet，只有配置了这个bean，监控才会开启
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        Optional<String> urlPattern = Optional.ofNullable(env.getProperty("spring.durid-datasource.stat-view-servlet.url-pattern"));
        Optional<String> enabled = Optional.ofNullable(env.getProperty("spring.durid-datasource.stat-view-servlet.enabled"));
        Optional<String> username = Optional.ofNullable(env.getProperty("spring.durid-datasource.stat-view-servlet.login-username"));
        Optional<String> password = Optional.ofNullable(env.getProperty("spring.durid-datasource.stat-view-servlet.login-password"));

        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), urlPattern.orElse("/druid/*"));
        Map<String,String> initParams = new HashMap<>();
        initParams.put("enabled", enabled.orElse("true"));
        initParams.put("loginUsername", username.orElse("admin"));
        initParams.put("loginPassword", password.orElse("123456"));
        initParams.put("allow", "");//默认就是允许所有访问

        bean.setInitParameters(initParams);
        return bean;
    }

    /**
     * 配置Durid监控后台SQL监控拦截器。只有 命中拦截规则的请求 发起的sql查询才会被监控到
     *
     * @return 返回过滤器配置对象
     */
    /*@Bean
    public FilterRegistrationBean statFilterDemo() {
        Optional<Boolean> enabled = Optional.ofNullable("true".equals(env.getProperty("spring.durid-datasource.web-stat-filter.enabled")));
        Optional<String> urlPattern = Optional.ofNullable(env.getProperty("spring.durid-datasource.web-stat-filter.url-pattern"));
        Optional<String> exclusions = Optional.ofNullable(env.getProperty("spring.durid-datasource.web-stat-filter.exclusions"));

        FilterRegistrationBean frb = new FilterRegistrationBean(new WebStatFilter());
        frb.setEnabled(enabled.orElse(false));
        // 添加过滤规则
        frb.addUrlPatterns(urlPattern.orElse("/*"));
        // 忽略过滤格式
        frb.addInitParameter("exclusions", exclusions.orElse("\"*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*\""));
        return frb;
    }*/


}
